<?php
/**
 * Created by PhpStorm.
 * User: lengbin
 * Date: 2018/8/2
 * Time: 下午9:05
 */

namespace common\core\component;

use JPush\Client;
use JPush\PushPayload;
use yii\base\Component;

/**
 * Class Jpush
 *
 * 极光推送
 *
 * @composer composer require --prefer-dist "jpush/jpush" "*" -vvv
 * @doc https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#message
 *
 * @package  common\helpers
 */
class Jpush extends Component
{

    public $key;

    public $secret;

    public $type;

    public $isProd;

    /**
     * @var Client $client
     */
    protected $client;

    /**
     * @var PushPayload
     */
    protected $push;

    protected $isSend;

    /**
     * @throws \Exception
     */
    public function init()
    {
        parent::init();
        if ($this->client === null) {
            $this->client = new Client($this->key, $this->secret);
        }

        if ($this->isSend === null) {
            $this->isSend = true;
        }

        $type = ['alias', 'tag', 'id', 'all'];
        if (!empty($this->type)) {
            if (!in_array($this->type, $type)) {
                throw new \Exception('jpush only support ' . implode(',', $type));
            }
        }

        if ($this->isProd === null) {
            $this->isProd = false;
        }
    }

    /**
     * 发送
     *
     * @param null $receiver
     *
     * @return array
     */
    protected function send($receiver = null)
    {
        $push = $this->push;
        switch ($this->type) {
            case 'alias':
                $push->addAlias($receiver);
                break;
            case 'tag':
                $push->addTag($receiver);
                break;
            case 'id':
                $push->addRegistrationId($receiver);
                break;
            default:
                $push->addAllAudience();
                break;
        }
        $push->options([
            "apns_production" => $this->isProd,
            'time_to_live'    => 86400,
        ]);
        try {
            return $push->send();
        } catch (\Exception $exception) {
            return [];
        }
    }

    /**
     * 发送 android 用户
     *
     * @param string/array $receiver 接收人 null，默认是所有人发送
     * @param string $content 内容
     * @param array  $extras  扩展
     * @param string $title   标题
     *
     * @return array|PushPayload
     */
    public function sendAndroid($receiver, $content, array $extras = [], $title = '')
    {
        if ($this->push === null) {
            $this->push = $this->client->push()->setPlatform('android');
        }
        $data = [];
        if (!empty($extras)) {
            $data['extras'] = $extras;
        }

        if (!empty($title)) {
            $data['title'] = $title;
        }
        $this->push = $this->push->androidNotification($content, $data);
        return $this->isSend ? $this->send($receiver) : $this->push;
    }

    /**
     * 发送 ios 用户
     *
     * @param string/array $receiver 接收人 null，默认是所有人发送
     * @param string $content 内容
     * @param array  $extras  扩展
     * @param string $title   标题
     *
     * @return array|PushPayload
     */
    public function sendIos($receiver, $content, array $extras = [], $title = '')
    {
        if ($this->push === null) {
            $this->push = $this->client->push()->setPlatform('ios');
        }
        $data = [
            'title' => $content,
        ];
        if (!empty($title)) {
            $data['subtitle'] = $title;
        }
        $this->push = $this->push->iosNotification($data, [
            'sound'             => 'default',
            'extras'            => $extras,
            'content-available' => true,
        ]);
        return $this->isSend ? $this->send($receiver) : $this->push;
    }

    /**
     * 自定内容发送， 如果接收人不填， 默认是所有人发送
     *
     * @param string $content  内容
     * @param array  $extras   其他信息
     * @param null   $receiver 接收人，默认是所有人发送
     * @param string $title    标题
     *
     * @return array
     */
    public function sendCustom($content, array $extras = [], $receiver = null, $title = '')
    {
        $data = [];
        if (!empty($title)) {
            $data['title'] = $title;
        }
        if (!empty($extras)) {
            $data['extras'] = $extras;
        }
        $this->push = $this->client->push()->setPlatform('all')->message($content, $data);
        return $this->send($receiver);
    }

    /**
     * 发送所有用户
     *
     * @param null   $receiver 接收人 null，默认是所有人发送
     * @param string $content  内容
     * @param array  $extras   扩展
     * @param string $title    标题
     *
     * @return array
     */
    public function sendAll($content, $receiver = null, array $extras = [], $title = '')
    {
        $this->isSend = false;
        $this->push = $this->client->push()->setPlatform('all');
        $this->push->setNotificationAlert($content);
        $this->sendIos($receiver, $content, $extras, $title);
        $this->sendAndroid($receiver, $content, $extras, $title);
        return $this->send($receiver);
    }

}